﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ValiVIN.Manufacturer
{
    internal class LamborghiniOLD : IVINManufacturer
    {
        //Example VIN: ZA9CA05A4KLA12514

        //Last Year
        private static readonly int _LAST_YEAR = 2003;

        #region Singleton

        static readonly LamborghiniOLD _instance = new LamborghiniOLD();

        public static LamborghiniOLD Instance
        {
            get { return _instance; }
        }

        #endregion

        private void CheckYear(VINumber vin)
        {
            if (vin.Year2 > _LAST_YEAR)
            {
                vin.Year2 = 0;
            }
        }

        #region IVINManufacturer Members

        public bool Decode(VINumber vin)
        {

            CheckYear(vin);
            GetPlant(vin);
            GetModel(vin);
            GetBody(vin);
            GetSerialNumber(vin);
            GetMarket(vin);
            GetEngine(vin);
            GetFeatures(vin);
            return true;
        }

        private void GetFeatures(VINumber vin)
        {
            //digit - 8: Features (B=Passenger Airbag, P=Passive Restraints, A=Active Restraints, U=USA, E=Europe, etc)
            string key = "Features";
            string value = string.Empty;
            string v = vin.VIN.Substring(7, 1);

            switch (v)
            {
                case "B":
                    value = "Passenger Airbag";
                    break;
                case "P":
                    value = "Passive Restraints";
                    break;
                case "A":
                    value = "Active Restraints";
                    break;
            }

            if (!string.IsNullOrEmpty(value))
            {
                vin.ManufacturerSpecifics[key] = value;
            }
        }

        private void GetEngine(VINumber vin)
        {
            //Digit 7 = Engine (1=500HPv10, 2=520HPv10, 3=530HPv10, 4=560HP/5.2v10, B=550HP/5.2v10, 6=6.2v12, 7=6.5v12)
            //Engine (0=3.5v8, 5=5.2v12, 7=5.7v12, 2=5.7v12VVT, 1=v12VVT, 0=6.2v12)
            string key = "Engine";
            string value = string.Empty;
            string v = vin.VIN.Substring(6, 1);

            switch (v)
            {
                case "0":
                    value = "3.5v8 or 6.2v12";
                    break;
                case "5":
                    value = "5.2v12";
                    break;
                case "7":
                    value = "5.7v12";
                    break;
                case "2":
                    value = "5.7v12VVT";
                    break;
                case "1":
                    value = "v12VVT";
                    break;
            }

            if (!string.IsNullOrEmpty(value))
            {
                vin.ManufacturerSpecifics[key] = value;
            }
        }

        private void GetMarket(VINumber vin)
        {
            //Digit 5 = Market (C=Canada **, E=Europe, U=USA, 0=other, A=USA, etc) 
            string key = "Market";
            string value = string.Empty;
            string v = vin.VIN.Substring(6, 1);

            switch (v)
            {
                case "C":
                    value = "Canada";
                    break;
                case "E":
                    value = "Europe";
                    break;
                case "U":
                case "A":
                    value = "USA";
                    break;
                case "0":
                    value = "Other";
                    break;
            }

            if (!string.IsNullOrEmpty(value))
            {
                vin.ManufacturerSpecifics[key] = value;
            }
        }

        private void GetSerialNumber(VINumber vin)
        {
            //15 - 17
            string serialNum = vin.VIN.Substring(14, 3);
            vin.SerialNumber = serialNum;
        }

        private void GetModel(VINumber vin)
        {
            //Model (C=Countach, D=Diablo, J=Jalpa, L=LM002, R=Diablo Roadster, B=Murci)
            string key = "Model";
            string model = string.Empty;
            string m = vin.VIN.Substring(3, 1); //digit 4

            if (m == "C")
            {
                model = "Countach";
            }
            else if (m == "D")
            {
                model = "Diablo";
            }
            else if (m == "J")
            {
                model = "Jalpa";
            }
            else if (m == "L")
            {
                model = "LM002";
            }
            else if (m == "R")
            {
                model = "Diablo Roadster";
            }
            else if (m == "B")
            {
                model = "Murciélago";
            }

            if (!string.IsNullOrEmpty(model))
            {
                vin.ManufacturerSpecifics[key] = model;
            }
        }

        private string GetBodyGallardo(VINumber vin)
        {
            //Body (Gallardo - 1=Coupe, 2=Spyder, 4=Superleggera, 5=LP5x0 Coupe, 6=LP560 Spyder)
            string body = string.Empty;
            string b = vin.VIN.Substring(5, 1); //digit 6

            switch (b)
            {
                case "1":
                    body = "Coupe";
                    break;
                case "2":
                    body = "Spyder";
                    break;
                case "4":
                    body = "Superleggera";
                    break;
                case "5":
                    body = "LP5x0 Coupe";
                    break;
                case "6":
                    body = "LP560 Spyder";
                    break;
            }

            return body;
        }

        private void GetBody(VINumber vin)
        {
            //Body (0=Coupe, 1=VT Coupe, 2=2wd Coupe, 3=Roadster, 4=SUV)
            string key = "Market";
            string value = string.Empty;
            string v = vin.VIN.Substring(5, 1);

            switch (v)
            {
                case "0":
                    value = "Coupe";
                    break;
                case "1":
                    value = "VT Coupe";
                    break;
                case "2":
                    value = "2wd Coupe";
                    break;
                case "3":
                    value = "Roadster";
                    break;
                case "4":
                    value = "SUV";
                    break;
            }

            if (!string.IsNullOrEmpty(value))
            {
                vin.ManufacturerSpecifics[key] = value;
            }
        }

        private void GetPlant(VINumber vin)
        {
            // Plant (L=Sant'Agata Bolognese - Bologna Italia)
            string plant = string.Empty;
            string p = vin.VIN.Substring(10, 1);
            if (p == "L")
            {
                plant = "Sant'Agata Bolognese - Bologna Italia";
            }

            vin.Plant = plant;
        }

        public string ManufacturerName(string vin)
        {
            return "Automobili Lamborghini";
        }

        #endregion
    }
}
